﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reborn.Net.Http.Drive
{
    public abstract class Drive
    {
        protected Request request = null;
        protected Response response = null;
        protected HttpConfig _httpConfig = null;


        public Drive(HttpConfig httpConfig)
        {
            request = new Request();
            response = new Response();
            _httpConfig = httpConfig;
        }

        public int Timeout { get; set; }

        /// <summary>
        /// 异步请求
        /// </summary>
        /// <returns></returns>
        public abstract Task<Response> GetAsync(string url, Request request = null);

        /// <summary>
        /// Get请求
        /// </summary>
        /// <returns></returns>
        public abstract Response Get(string url, Request request = null);


        public abstract byte[] GetByte(string url);


        public abstract Task<byte[]> GetByteAsync(string url);

        /// <summary>
        /// POST请求
        /// </summary>
        /// <returns></returns>
        public abstract Response Post(string url, string token, Request request = null);


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name=""></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public abstract Response Post(string url, Dictionary<string, string> dicArgs, Request request = null);

        /// <summary>
        /// POST异步请求
        /// </summary>
        /// <returns></returns>
        public abstract Task<Response> PostAsync(string url, string token, Request request = null);

        /// <summary>
        /// POST异步请求
        /// </summary>
        /// <returns></returns>
        public abstract Task<Response> PostAsync(string url, Dictionary<string, string> dicArgs, Request request = null);

        /// <summary>
        /// 请求JSON
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public abstract Response PostJson(string url, JToken postArgs, Request request = null);



        public abstract Task<Response> PostJsonAsync(string url, JToken postArgs, Request request = null);





        public string JsonToString(object obj)
        {

            return JsonConvert.SerializeObject(obj);
        }





        public string DictionaryToString(Dictionary<string, string> dic, string json = "&")
        {
            List<string> strings = new List<string>();
            foreach (var item in dic)
            {

                string key = item.Key;
                string value = item.Value;

                strings.Add(string.Format("{0}={1}", key, value));
            }
            return string.Join(json, strings);
        }


        public JToken ToJson(string val)
        {
            try
            {
                return JToken.Parse(val);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
